﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using Com_Orion_Test_DLL;
using System.Xml.Serialization;

namespace Com_Orion_TestRUNMakeTool
{
    public partial class Form1 : Form
    {
        private List<string> FilenameList;
        public Form1()
        {
            InitializeComponent();
        }

        private void TestFileMake_CBox_CheckedChanged(object sender, EventArgs e)
        {
            SetFileSendFlg(TestFileMake_CBox.Checked);
        }
        private void SetFileSendFlg(bool check)
        {
            TestFilePath_TBox.Enabled = check;
            TestContNumMake_CBox.Enabled = check;
            SelFol_Btn.Enabled = check;
            FileTopMes_TBox.Enabled = check;
        }

        private void SelFol_Btn_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog FDB = new FolderBrowserDialog();
            FDB.SelectedPath = System.IO.Directory.GetCurrentDirectory();
            if (FDB.ShowDialog() == DialogResult.OK)
            {
                TestFilePath_TBox.Text = FDB.SelectedPath;
            }
        }

        private void Make_Btn_Click(object sender, EventArgs e)
        {
            if (!CheckPath())
            {
                return;
            }
            if (TestFileMake_CBox.Checked)
                MakeFile();
            else
                FilenameList = new List<string>(Int32.Parse(OutPutNum_TBox.Text));
            SaveFileDialog xmlOpenDlg = new SaveFileDialog();
            xmlOpenDlg.FileName = "setting.xml";
            xmlOpenDlg.InitialDirectory = System.IO.Directory.GetCurrentDirectory();
            xmlOpenDlg.Filter = "XMLファイル(*.xml)|*.xml";
            if (xmlOpenDlg.ShowDialog() == DialogResult.OK)
            {
                MakeXml(xmlOpenDlg.FileName);
            }
        }
        private bool CheckPath()
        {
            if (String.IsNullOrEmpty(ExePath_TBox.Text) || String.IsNullOrEmpty(ComParm_TBox.Text) || String.IsNullOrEmpty(OutPutNum_TBox.Text))
            {
                MessageBox.Show("未入力パラメーターがあります。");
                return false;
            }
            if (TestFileMake_CBox.Checked && (String.IsNullOrEmpty(TestFilePath_TBox.Text) || String.IsNullOrEmpty(FileTopMes_TBox.Text)))
            {
                MessageBox.Show("未入力パラメーターがあります。");
                return false;
            }
            int num;
            if (!Int32.TryParse(OutPutNum_TBox.Text, out num) || num <= 0)
            {
                MessageBox.Show("実行回数は1以上の数字でお願いします。");
                return false;
            }
            
            return true;
        }
        private void MakeFile()
        {
            FilenameList = new List<string>();
            for (int i = 0; i < Int32.Parse(OutPutNum_TBox.Text); i++)
            {
                string file_name = @TestFilePath_TBox.Text; // ファイル名の指定
                file_name += @"\";
                file_name += @FileTopMes_TBox.Text;
                file_name += "_";
                file_name += i.ToString();
                FilenameList.Add(file_name);
                if (TestContNumMake_CBox.Checked == false)
                {
                    StreamWriter writer = new StreamWriter(file_name, false, System.Text.Encoding.UTF8);
                    writer.WriteLine(file_name);
                    writer.Close();
                }
            }
        }
        private void MakeXml(string Savefilename)
        {
            XMLINFO tmpXMLINFO = new XMLINFO(Int32.Parse(OutPutNum_TBox.Text));
            //XMLINFO tmpXMLINFO = new XMLINFO();
            if (TestFileMake_CBox.Checked)
            {
                int i = 0;
                foreach (String FN in FilenameList)
                {
                    TestProgramInfo TestPG = new TestProgramInfo();
                    TestPG.ProgramFullPath = ExePath_TBox.Text;
                    TestPG.ProgramArg = ComParm_TBox.Text;
                    TestPG.ProgramArg += " " + FN;
                    TestPG.ProgramName = "TestProgram";
                    tmpXMLINFO.TestPG[i] = TestPG;
                    i++;
                }
            }
            else
            {
                for (int i = 0; i < Int32.Parse(OutPutNum_TBox.Text); i++)
                {
                    TestProgramInfo TestPG = new TestProgramInfo();
                    TestPG.ProgramFullPath = ExePath_TBox.Text;
                    TestPG.ProgramArg = ComParm_TBox.Text;
                    TestPG.ProgramName = "TestProgram";
                    tmpXMLINFO.TestPG[i] = TestPG;
                }
            }
            using (FileStream fs1 = new FileStream(@Savefilename, System.IO.FileMode.Create))
            {
                XmlSerializer writeXml = new XmlSerializer(typeof(XMLINFO));
                writeXml.Serialize(fs1, tmpXMLINFO);
                fs1.Close();
            }
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog xmlOpenDlg = new OpenFileDialog();
            xmlOpenDlg.InitialDirectory = System.IO.Directory.GetCurrentDirectory();
            xmlOpenDlg.Filter = "exeファイル(*.exe)|*.exe|すべてのファイル(*.*)|*.*";
            if (xmlOpenDlg.ShowDialog() == DialogResult.OK)
            {
                ExePath_TBox.Text = xmlOpenDlg.FileName;
            }
        }

        private void SelFol_Btn_Click_1(object sender, EventArgs e)
        {

        }


    }
}
